package zoo.member.controller;


import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import zoo.member.dao.MemberDao;
import zoo.member.domain.MemberCommand;
import zoo.member.validator.MemberJoinValidator;

@Controller
public class MemberJoinPageController {
	@Autowired
	private MemberDao memberDao;
	private String formViewName = "memberjoin";
	
	@RequestMapping(value="/main/memberjoin.do",method=RequestMethod.GET)
	public String form() {
			return formViewName;
	}
	@ModelAttribute("command")
	public MemberCommand formBacking(){
		return new MemberCommand();
	}
	/*@ModelAttribute("phoneTypes")
	protected List<String> referenceData() throws Exception{
		List<String> phoneTypes =new ArrayList<String>();
		phoneTypes.add("010");
		phoneTypes.add("011");
		phoneTypes.add("017");
		phoneTypes.add("018");
		phoneTypes.add("019");
		
		return phoneTypes;
	}
	@ModelAttribute("emailTypes")
	protected List<String> referenceData2() throws Exception{
		List<String> emailTypes =new ArrayList<String>();
		
		emailTypes.add("chol.com");
		emailTypes.add("dreamwiz.com");
		emailTypes.add("hanmir.com");
		emailTypes.add("hanafos.com");
		emailTypes.add("hotmail.com");
		emailTypes.add("lycos.co.kr");
		emailTypes.add("nate.com");
		emailTypes.add("naver.com");
		emailTypes.add("daum.net");
		emailTypes.add("hanmail.net");
		emailTypes.add("gmail.com");
		emailTypes.add("daum.net");
		emailTypes.add("paran.com");
		emailTypes.add("yahoo.co.kr");
		
		return emailTypes;
	}*/
	@ModelAttribute("memberTypes")
	protected List<String> referenceData3() throws Exception{
		List<String> memberTypes =new ArrayList<String>();
		memberTypes.add("정상회원");
		
		
		return memberTypes;
	}
	@RequestMapping(value="/main/memberjoin.do",method=RequestMethod.POST)
	public String submit(@ModelAttribute("command") MemberCommand memberCommand,
							BindingResult result){
		
		new MemberJoinValidator().validate(memberCommand, result);
		MemberCommand com = memberDao.getMember(memberCommand.getUserid());
		
		if(result.hasErrors()){
			return formViewName;
		}
		
		if(com != null){
			result.rejectValue("userid","required.useridCheck");
			return formViewName;
		}
		/*String umail = memberCommand.getUemail().replaceAll(",", "");
		memberCommand.setUemail(umail);
		
		String upphone = memberCommand.getUphone().replaceAll(",", "");
		memberCommand.setUphone(upphone);*/
		
		memberDao.insert(memberCommand);
		return "redirect:/main/index.do";
	}
}